package com.ailk.kbi.kbirazor.component
{
	import com.ailk.kbi.kbirazor.common.CONTENT;
	import com.ailk.kbi.kbirazor.vo.KpiVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.core.DragSource;
	import mx.core.EventPriority;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import spark.components.HGroup;
	import spark.components.Label;
	import spark.components.SkinnableContainer;
	import spark.components.SkinnableDataContainer;
	import spark.formatters.NumberFormatter;
	
	/**
	 * KPI容器盒子
	 * 
	 */
	public class KpiBox extends HGroup
	{
		private var _kpiVO:KpiVO;
		
		[Bindable]
		private var _kpiXML:XML;
		
		/*显示名称*/
		private var _lbKpiName:Label;
		
		/*显示值*/
		private var _lbKpiValue:Label;
		
		/*显示单位*/
		private var _lbKpiUnit:Label;
		
		private var numberFormat:NumberFormatter = new NumberFormatter();
		
		public function KpiBox(kpiXML:XML)
		{
			super();
			_kpiXML = kpiXML;
			updateChildren();
			this.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			
		}
		
		private function updateChildren():void{
			numberFormat.fractionalDigits = 2;
			numberFormat.groupingSeparator = ",";
			numberFormat.useGrouping = true;
			
			_lbKpiName = new Label();
			if(!_lbKpiName.hasEventListener("widthChanged")){
				_lbKpiName.addEventListener("widthChanged", widthChangedHandler);
			}
			if(!_lbKpiName.hasEventListener("heightChanged")){
				_lbKpiName.addEventListener("heightChanged", heightChangedHandler);
			}
			
			try{
				_kpiVO = new KpiVO();
				_kpiVO.kpiId = _kpiXML.@id;
				_kpiVO.kpiName = _kpiXML.@name;	
				
				//				_kpiVO.kpiValue = _kpiXML.@kpiValue;
				_kpiVO.kpiUnit = _kpiXML.@kpi_unit;
				
				_kpiVO.cityId = _kpiXML.@cityId;
				_kpiVO.areaId = _kpiXML.@areaId;
				_kpiVO.cityName = _kpiXML.@cityName;
				_kpiVO.opTime = _kpiXML.@opTime;
				
				_kpiVO.brandFlag = (Number)(_kpiXML.@brand_flag);
				_kpiVO.divisionFlag =(Number)( _kpiXML.@division_flag);
				if(_kpiVO.divisionFlag == 1) {
					_kpiVO.kpiValue = _kpiXML.@kpiValue;
				} else if(_kpiVO.divisionFlag == 2) {
					_kpiVO.kpiValue = (Number)(numberFormat.format(_kpiXML.@kpiValue1 / _kpiXML.@kpiValue2));
				} else {
					_kpiVO.kpiValue = (Number)(numberFormat.format(_kpiXML.@kpiValue1 * 100 / _kpiXML.@kpiValue2));
				}

				_lbKpiName.text = _kpiVO.kpiName + ":" + _kpiVO.kpiValue + "" + _kpiVO.kpiUnit;
				
				
				if(_kpiXML.hasOwnProperty("@hb_flag")) {
					_kpiVO.riseValue = _kpiXML.@riseValue;
					_lbKpiName.text = _lbKpiName.text + "\n环比净增" +":" +  _kpiVO.riseValue + "" + _kpiVO.kpiUnit;
				}
				
				
				if(_kpiXML.hasOwnProperty('@warn_flag') && _kpiXML.@warn_flag == '1') {
					_lbKpiName.setStyle("color",CONTENT.COLOR_WARN);
				}
				
				
				addElement(_lbKpiName);
				_lbKpiName.validateSize(true);
				
				
			}
			catch(error:Error){
				Alert.show(error.message);
			}
		}
		
		private function mouseMoveHandler(event:MouseEvent):void {
			var dragInitiator:HGroup=HGroup(event.currentTarget);
			var ds:DragSource = new DragSource();
			ds.addData(_kpiVO, 'KpiVO');
			DragManager.doDrag(dragInitiator, ds, event);
		}
		
		private function widthChangedHandler(event:Event):void {
			this.width = _lbKpiName.width;
		}
		
		private function heightChangedHandler(event:Event):void {
			this.height = _lbKpiName.height;
		}
		
	}
}